/* 
 * EasyUI DataGrid DatailView. 
 * right by Jiance Qin 
 * version v1.0 
 * 
 * */
// remote data format
function DgData(total,rows)
{
		this.total=total;
		this.rows=rows;
}

var detailview = $.extend({}, $.fn.datagrid.defaults.view, {
	render: function(target, container, frozen){
		var state = $.data(target, 'datagrid');
		var opts = state.options;
		if (frozen){
			if (!(opts.rownumbers || (opts.frozenColumns && opts.frozenColumns.length))){
				return;
			}
		}
		
		var rows = state.data.rows;
		var fields = $(target).datagrid('getColumnFields', frozen);
		var table = [];
		table.push('<table class="datagrid-btable" cellspacing="0" cellpadding="0" border="0"><tbody>');
		for(var i=0; i<rows.length; i++) {
			// get the class and style attributes for this row
			var css = opts.rowStyler ? opts.rowStyler.call(target, i, rows[i]) : '';
			var classValue = '';
			var styleValue = '';
			if (typeof css == 'string'){
				styleValue = css;
			} else if (css){
				classValue = css['class'] || '';
				styleValue = css['style'] || '';
			}
			
			var cls = 'class="datagrid-row ' + (i % 2 && opts.striped ? 'datagrid-row-alt ' : ' ') + classValue + '"';
			var style = styleValue ? 'style="' + styleValue + '"' : '';
			var rowId = state.rowIdPrefix + '-' + (frozen?1:2) + '-' + i;
			table.push('<tr id="' + rowId + '" datagrid-row-index="' + i + '" ' + cls + ' ' + style + '>');
			table.push(this.renderRow.call(this, target, fields, frozen, i, rows[i]));
			table.push('</tr>');
			
			table.push('<tr style="display:none;">');
			if (frozen){
				table.push('<td colspan=' + (fields.length+2) + ' style="border-right:0">');
			} else {
				table.push('<td colspan=' + (fields.length) + '>');
			}
			table.push('<div class="datagrid-row-detail">');
			if (frozen){
				table.push('&nbsp;');
			} else {
				table.push(opts.detailFormatter.call(target, i, rows[i]));
			}
			table.push('</div>');
			table.push('</td>');
			table.push('</tr>');
			
		}
		table.push('</tbody></table>');
		
		$(container).html(table.join(''));
	},
	
	renderRow: function(target, fields, frozen, rowIndex, rowData){
		var opts = $.data(target, 'datagrid').options;
		
		var cc = [];
		if (frozen && opts.rownumbers){
			var rownumber = rowIndex + 1;
			if (opts.pagination){
				rownumber += (opts.pageNumber-1)*opts.pageSize;
			}
			cc.push('<td class="datagrid-td-rownumber"><div class="datagrid-cell-rownumber">'+rownumber+'</div></td>');
		}
		for(var i=0; i<fields.length; i++){
			var field = fields[i];
			var col = $(target).datagrid('getColumnOption', field);
			if (col){
				var value = rowData[field];	// the field value
				var css = col.styler ? (col.styler(value, rowData, rowIndex)||'') : '';
				var classValue = '';
				var styleValue = '';
				if (typeof css == 'string'){
					styleValue = css;
				} else if (cc){
					classValue = css['class'] || '';
					styleValue = css['style'] || '';
				}
				var cls = classValue ? 'class="' + classValue + '"' : '';
				var style = col.hidden ? 'style="display:none;' + styleValue + '"' : (styleValue ? 'style="' + styleValue + '"' : '');
				
				cc.push('<td field="' + field + '" ' + cls + ' ' + style + '>');
				
				if (col.checkbox){
					style = '';
				} else if (col.expander){
					style = "text-align:center;height:16px;";
				} else {
					style = styleValue;
					if (col.align){style += ';text-align:' + col.align + ';'}
					if (!opts.nowrap){
						style += ';white-space:normal;height:auto;';
					} else if (opts.autoRowHeight){
						style += ';height:auto;';
					}
				}
				
				cc.push('<div style="' + style + '" ');
				if (col.checkbox){
					cc.push('class="datagrid-cell-check ');
				} else {
					cc.push('class="datagrid-cell ' + col.cellClass);
				}
				cc.push('">');
				
				if (col.checkbox){
					cc.push('<input type="checkbox" name="' + field + '" value="' + (value!=undefined ? value : '') + '">');
				} else if (col.expander) {
					//cc.push('<div style="text-align:center;width:16px;height:16px;">');
					cc.push('<span class="datagrid-row-expander datagrid-row-expand" style="display:inline-block;width:16px;height:16px;cursor:pointer;" />');
					//cc.push('</div>');
				} else if (col.formatter){
					cc.push(col.formatter(value, rowData, rowIndex));
				} else {
					cc.push(value);
				}
				
				cc.push('</div>');
				cc.push('</td>');
			}
		}
		return cc.join('');
	},
	
	insertRow: function(target, index, row){
		var opts = $.data(target, 'datagrid').options;
		var dc = $.data(target, 'datagrid').dc;
		var panel = $(target).datagrid('getPanel');
		var view1 = dc.view1;
		var view2 = dc.view2;
		
		var isAppend = false;
		var rowLength = $(target).datagrid('getRows').length;
		if (rowLength == 0){
			$(target).datagrid('loadData',{total:1,rows:[row]});
			return;
		}
		
		if (index == undefined || index == null || index >= rowLength) {
			index = rowLength;
			isAppend = true;
			this.canUpdateDetail = false;
		}
		
		$.fn.datagrid.defaults.view.insertRow.call(this, target, index, row);
		
		_insert(true);
		_insert(false);
		
		this.canUpdateDetail = true;
		
		function _insert(frozen){
			var v = frozen ? view1 : view2;
			var tr = v.find('tr[datagrid-row-index='+index+']');
			
			if (isAppend){
				var newDetail = tr.next().clone();
				tr.insertAfter(tr.next());
			} else {
				var newDetail = tr.next().next().clone();
			}
			newDetail.insertAfter(tr);
			newDetail.hide();
			if (!frozen){
				newDetail.find('div.datagrid-row-detail').html(opts.detailFormatter.call(target, index, row));
			}
		}
	},
	
	deleteRow: function(target, index){
		var opts = $.data(target, 'datagrid').options;
		var dc = $.data(target, 'datagrid').dc;
		var tr = opts.finder.getTr(target, index);
		tr.next().remove();
		$.fn.datagrid.defaults.view.deleteRow.call(this, target, index);
		dc.body2.triggerHandler('scroll');
	},
	
	updateRow: function(target, rowIndex, row){
		var dc = $.data(target, 'datagrid').dc;
		var opts = $.data(target, 'datagrid').options;
		var cls = $(target).datagrid('getExpander', rowIndex).attr('class');
		$.fn.datagrid.defaults.view.updateRow.call(this, target, rowIndex, row);
		$(target).datagrid('getExpander', rowIndex).attr('class',cls);
		
		// update the detail content
		if (this.canUpdateDetail){
			var row = $(target).datagrid('getRows')[rowIndex];
			var detail = $(target).datagrid('getRowDetail', rowIndex);
			detail.html(opts.detailFormatter.call(target, rowIndex, row));
		}
	},
	
	bindEvents: function(target){
		var state = $.data(target, 'datagrid');
		var dc = state.dc;
		var opts = state.options;
		var body = dc.body1.add(dc.body2);
		var clickHandler = ($.data(body[0],'events')||$._data(body[0],'events')).click[0].handler;
		body.unbind('click').bind('click', function(e){
			var tt = $(e.target);
			var tr = tt.closest('tr.datagrid-row');
			if (!tr.length){return}
			if (tt.hasClass('datagrid-row-expander')){
				var rowIndex = parseInt(tr.attr('datagrid-row-index'));
				if (tt.hasClass('datagrid-row-expand')){
					$(target).datagrid('expandRow', rowIndex);
				} else {
					$(target).datagrid('collapseRow', rowIndex);
				}
				$(target).datagrid('fixRowHeight');
				
			} else {
				clickHandler(e);
			}
			e.stopPropagation();
		});
	},
	
	onBeforeRender: function(target){
		var state = $.data(target, 'datagrid');
		var opts = state.options;
		var dc = state.dc;
		var t = $(target);
		var hasExpander = false;
		var fields = t.datagrid('getColumnFields',true).concat(t.datagrid('getColumnFields'));
		for(var i=0; i<fields.length; i++){
			var col = t.datagrid('getColumnOption', fields[i]);
			if (col.expander){
				hasExpander = true;
				break;
			}
		}
		if (!hasExpander){
			if (opts.frozenColumns && opts.frozenColumns.length){
				opts.frozenColumns[0].splice(0,0,{field:'_expander',expander:true,width:24,resizable:false,fixed:true});
			} else {
				opts.frozenColumns = [[{field:'_expander',expander:true,width:24,resizable:false,fixed:true}]];
			}
			
			var t = dc.view1.children('div.datagrid-header').find('table');
			var td = $('<td rowspan="'+opts.frozenColumns.length+'"><div class="datagrid-header-expander" style="width:24px;"></div></td>');
			if ($('tr',t).length == 0){
				td.wrap('<tr></tr>').parent().appendTo($('tbody',t));
			} else if (opts.rownumbers){
				td.insertAfter(t.find('td:has(div.datagrid-header-rownumber)'));
			} else {
				td.prependTo(t.find('tr:first'));
			}
		}
		
		var that = this;
		setTimeout(function(){
			that.bindEvents(target);
		},0);
	},
	
	onAfterRender: function(target){
		var that = this;
		var state = $.data(target, 'datagrid');
		var dc = state.dc;
		var opts = state.options;
		var panel = $(target).datagrid('getPanel');
		
		$.fn.datagrid.defaults.view.onAfterRender.call(this, target);
		
		if (!state.onResizeColumn){
			state.onResizeColumn = opts.onResizeColumn;
		}
		if (!state.onResize){
			state.onResize = opts.onResize;
		}
		function setBodyTableWidth(){
			var columnWidths = dc.view2.children('div.datagrid-header').find('table').width();
			dc.body2.children('table').width(columnWidths);
		}
		
		opts.onResizeColumn = function(field, width){
			setBodyTableWidth();
			var rowCount = $(target).datagrid('getRows').length;
			for(var i=0; i<rowCount; i++){
				$(target).datagrid('fixDetailRowHeight', i);
			}
			
			// call the old event code
			state.onResizeColumn.call(target, field, width);
		};
		opts.onResize = function(width, height){
			setBodyTableWidth();
			state.onResize.call(panel, width, height);
		};
		
		this.canUpdateDetail = true;	// define if to update the detail content when 'updateRow' method is called;
		
		dc.footer1.find('span.datagrid-row-expander').css('visibility', 'hidden');
		$(target).datagrid('resize');
	}
});

$.extend($.fn.datagrid.methods, {
	fixDetailRowHeight: function(jq, index){
		return jq.each(function(){
			var opts = $.data(this, 'datagrid').options;
			if (!(opts.rownumbers || (opts.frozenColumns && opts.frozenColumns.length))){
				return;
			}
			var dc = $.data(this, 'datagrid').dc;
			var tr1 = opts.finder.getTr(this, index, 'body', 1).next();
			var tr2 = opts.finder.getTr(this, index, 'body', 2).next();
			// fix the detail row height
			if (tr2.is(':visible')){
				tr1.css('height', '');
				tr2.css('height', '');
				var height = Math.max(tr1.height(), tr2.height());
				tr1.css('height', height);
				tr2.css('height', height);
			}
			dc.body2.triggerHandler('scroll');
		});
	},
	getExpander: function(jq, index){	// get row expander object
		var opts = $.data(jq[0], 'datagrid').options;
		return opts.finder.getTr(jq[0], index).find('span.datagrid-row-expander');
	},
	// get row detail container
	getRowDetail: function(jq, index){
		var opts = $.data(jq[0], 'datagrid').options;
		var tr = opts.finder.getTr(jq[0], index, 'body', 2);
		return tr.next().find('div.datagrid-row-detail');
	},
	expandRow: function(jq, index){
		return jq.each(function(){
			var opts = $(this).datagrid('options');
			var dc = $.data(this, 'datagrid').dc;
			var expander = $(this).datagrid('getExpander', index);
			if (expander.hasClass('datagrid-row-expand')){
				expander.removeClass('datagrid-row-expand').addClass('datagrid-row-collapse');
				var tr1 = opts.finder.getTr(this, index, 'body', 1).next();
				var tr2 = opts.finder.getTr(this, index, 'body', 2).next();
				tr1.show();
				tr2.show();
				$(this).datagrid('fixDetailRowHeight', index);
				if (opts.onExpandRow){
					var row = $(this).datagrid('getRows')[index];
					opts.onExpandRow.call(this, index, row);
				}
			}
		});
	},
	collapseRow: function(jq, index){
		return jq.each(function(){
			var opts = $(this).datagrid('options');
			var dc = $.data(this, 'datagrid').dc;
			var expander = $(this).datagrid('getExpander', index);
			if (expander.hasClass('datagrid-row-collapse')){
				expander.removeClass('datagrid-row-collapse').addClass('datagrid-row-expand');
				var tr1 = opts.finder.getTr(this, index, 'body', 1).next();
				var tr2 = opts.finder.getTr(this, index, 'body', 2).next();
				tr1.hide();
				tr2.hide();
				dc.body2.triggerHandler('scroll');
				if (opts.onCollapseRow){
					var row = $(this).datagrid('getRows')[index];
					opts.onCollapseRow.call(this, index, row);
				}
			}
		});
	}
});

function createDatagrid(options) {
	if(!options || !options.expendFormId){
		return;
	}
	var targetId = options.targetId;
	var expendFormId = options.expendFormId;
	$('#'+targetId).datagrid({
		fit : (options.fit==false)?false:true,
		url : options.listUrl?options.listUrl:null, 
		idField : options.idField?options.idField:'id', 
		toolbar : options.toolBarId?("#" + options.toolBarId):null,
		pagination : (options.pagination==false)?false:true,
		fitColumns : (options.fitColumns==false)?false:true,
		singleSelect : (options.singleSelect==false)?false:true,
		rownumbers : (options.rownumbers==false)?false:true,
		sortName : options.sortName?options.sortName:null,
		sortOrder : (options.sortOrder=='desc')?'desc':'asc',
		pageSize : options.pageSize?options.pageSize:'10',
		view : detailview,
		detailFormatter : function(index, row) {
			return '<div class="ddv"></div>';
		},
		onExpandRow : function(index, row) {
			var ddv = $(this).datagrid('getRowDetail', index).find('div.ddv');
			ddv.panel({
				border : false,
				cache : true,
				content : options.expendFormString?options.expendFormString:null
			});
			
			// canUpdateAndAdd || canUpdateOnly || canAddOnly, will bind the save button.
			if((options.updateUrl && options.saveButtonId && options.newButtonId && options.addUrl)
					||(options.updateUrl && options.saveButtonId && (!options.addUrl || !options.newButtonId) && !($('#'+targetId).datagrid('getRows')[index].isNewRecord))
					||(!options.updateUrl && options.saveButtonId && options.addUrl && options.newButtonId && ($('#'+targetId).datagrid('getRows')[index].isNewRecord))
					){
				
				$('#'+targetId).datagrid('getRowDetail', index).find('#'+options.saveButtonId).bind("click",function(){
					saveItem(targetId,index,options.addUrl,options.updateUrl,expendFormId);
				});
			}
			// canAddOnly, disable update form's save button or linkButton
			if(!options.updateUrl && options.saveButtonId && options.addUrl && options.newButtonId && !($('#'+targetId).datagrid('getRows')[index].isNewRecord)){
				try{
					$('#'+targetId).datagrid('getRowDetail', index).find('#'+options.saveButtonId).linkbutton('disable');
				}catch(err){
					$('#'+targetId).datagrid('getRowDetail', index).find('#'+options.saveButtonId).attr("disabled","disabled");
				}
			}
			
			// canCancel
			if(options.cancelButtonId){
				$('#'+targetId).datagrid('getRowDetail', index).find('#'+options.cancelButtonId).bind("click",function(){
					cancelItem(targetId,index);
				});
			}
			
			$('#'+targetId).datagrid('fixDetailRowHeight', index);
			$('#'+targetId).datagrid('selectRow', index);
			$('#'+targetId).datagrid('getRowDetail', index).find('#'+expendFormId).form('load', row);
			if(options.onExpandRow){
				options.onExpandRow(index,row,targetId,expendFormId);
			}
			$('#'+targetId).datagrid('fixDetailRowHeight', index);
		},
		onBeforeLoad:function(param){
			hideSysMsg();
			if(options.onBeforeLoad){
				return options.onBeforeLoad(param);
			}else{
				return true;
			}
		},
		onSelect : function(rowIndex, rowData){
			if(options.onSelect){
				options.onSelect(rowIndex, rowData);
			}
		},
		loadFilter: function(data){
			var realData;
			if(typeof data.length=="number"&&typeof data.splice=="function"){ // local data in array type
				realData = {total:data.length,rows:data};
			}else if(data.total!=undefined&&data.rows!=undefined){ // local data
				realData = data;
			}else if(data.code!=undefined){ // remote data
				if (data.code==0){
					realData = data.data;
					if(typeof realData.length=="number"&&typeof realData.splice=="function"){ // remote data in array type
						realData = {total:realData.length,rows:realData};
					}
					if(!realData){
						realData = {total:0,rows:new Array()};
					}else{
						if(!realData.total){
							realData.total = 0;
						}
						if(!realData.rows){
							realData.rows = new Array();
						}
					}
				} else{
					showSysMsg(data.message);
					realData = {total:0,rows:new Array()};
				}
			}else{ // data format not support
				if(!options.dataFormatError){
					options.dataFormatError='Data format error!';
				}
				showSysMsg(options.dataFormatError);
				realData = {total:0,rows:new Array()};
			}
			return realData;
		},
		onLoadSuccess : function(data){
			if(options.onLoadSuccess){
				options.onLoadSuccess(data);
			}
		}
	});
	
	// bind reload button id
	if(options.reloadButtonId){
		$("#"+options.reloadButtonId).bind("click",function(){
			$('#'+targetId).datagrid('reload');
		});
	}
		
	// canAdd
	if(options.newButtonId){
		$("#"+options.newButtonId).bind("click",function(){
			newItem(targetId);
		});
	}
	
	// canAdd
	if(!options.confirmTitle){
		options.confirmTitle = 'Confirm';
	}
	if(!options.confirmMsg){
		options.confirmMsg = 'Are you sure to do it?';
	}
	if(!options.sysError){
		options.sysError = 'Server Error, please contact the server admin.';
	}
	if(options.deleteButtonId && options.deleteUrl){
		$("#"+options.deleteButtonId).bind("click",function(){
			destroyItem(targetId,options.deleteUrl,options.confirmTitle,options.confirmMsg,options.sysError);
		});
	}
}
function saveItem(targetId,index,addUrl,updateUrl,expendFormId) {
	var row = $('#'+targetId).datagrid('getRows')[index];
	var opts = $('#'+targetId).datagrid('options');
	var url = row.isNewRecord ? addUrl: (updateUrl + '?' + opts.idField + '=' + row[opts.idField]);
	$('#'+targetId).datagrid('getRowDetail', index).find('#'+expendFormId).form('submit', {
		url : url,
		onSubmit : function() {
			hideSysMsg();
			if($(this).form('validate')){
				$('#'+targetId).datagrid('loading');
				return true;
			}else{
				return false;
			}
		},
		success : function(data) {
			data = JSON.parse(data);
			if (data.code==0){
				var realData = data.data;
				realData.isNewRecord = false;
				$('#'+targetId).datagrid('collapseRow', index);
				$('#'+targetId).datagrid('updateRow', {
					index : index,
					row : realData
				});
			} else{
				showSysMsg(data.message);
			}
			$('#'+targetId).datagrid('loaded');
		}
	});
}
function cancelItem(targetId,index) {
	var row = $('#'+targetId).datagrid('getRows')[index];
	if (row.isNewRecord) {
		$('#'+targetId).datagrid('deleteRow', index);
	} else {
		$('#'+targetId).datagrid('collapseRow', index);
	}
}
function destroyItem(targetId,deleteUrl,confirmTitle,confirmMsg,sysError) {
	hideSysMsg();
	var row = $('#'+targetId).datagrid('getSelected');
	var opts = $('#'+targetId).datagrid('options');
	if (row) {
		$.messager.confirm(confirmTitle, confirmMsg, function(r) {
			if (r) {
				var index = $('#'+targetId).datagrid('getRowIndex',row);
				var dataObj={};
				dataObj[opts.idField]=row[opts.idField];
				$.ajax({
					type : "post",
					url : deleteUrl,
					data : dataObj,
					dataType : "json",
					error : function() {
						showSysMsg(sysError);
					},
					success : function(data) {
						if (data.code==0){
							$('#'+targetId).datagrid('deleteRow', index);
						} else{
							showSysMsg(data.message);
						}
					},
					beforeSend : function(xhr) {
						hideSysMsg();
						$('#'+targetId).datagrid('loading');
					},
					complete : function(xhr, ts) {
						$('#'+targetId).datagrid('loaded');
					}
				});
			}
		});
	}
}
function newItem(targetId) {
	$('#'+targetId).datagrid('appendRow', {
		isNewRecord : true
	});
	var index = $('#'+targetId).datagrid('getRows').length - 1;
	$('#'+targetId).datagrid('expandRow', index);
	$('#'+targetId).datagrid('selectRow', index);
}
function showSysMsg(msg){
	$("#sysMsg #msgText").html(msg);
	$("#sysMsg").show();
}
function hideSysMsg(){
	$("#sysMsg #msgText").empty();
	$("#sysMsg").hide();
}
function startProgress(){
	$.messager.progress({
		interval: 100,
		text: 'Loading...'
    });
}
function closeProgress(){
	$.messager.progress('close');
}

function formatDate(date, format) {   
    if (!date) return;   
    if (!format) format = "yyyy-MM-dd";   
    switch(typeof date) {   
        case "string":   
            date = new Date(date.replace(/-/, "/"));   
            break;   
        case "number":   
            date = new Date(date);   
            break;   
    }    
    if (!date instanceof Date) return;   
    var dict = {   
        "yyyy": date.getFullYear(),   
        "M": date.getMonth() + 1,   
        "d": date.getDate(),   
        "H": date.getHours(),   
        "m": date.getMinutes(),   
        "s": date.getSeconds(),   
        "MM": ("" + (date.getMonth() + 101)).substr(1),   
        "dd": ("" + (date.getDate() + 100)).substr(1),   
        "HH": ("" + (date.getHours() + 100)).substr(1),   
        "mm": ("" + (date.getMinutes() + 100)).substr(1),   
        "ss": ("" + (date.getSeconds() + 100)).substr(1)   
    };       
    return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function() {   
        return dict[arguments[0]];   
    });                   
}  

function formatMoneyFen(money) {
	var mymoney=String(money);
	var mymoneyArr=[];
	var index=mymoney.length;
	if(index>=3){
		mymoneyArr.push(mymoney.slice(index-2,index));
		index-=2;
		while(index>=3){
			mymoneyArr.push(mymoney.slice(index-3,index));
			index-=3;
		}
		if(index>0){
			mymoneyArr.push(mymoney.slice(0,index));
		}
		var mynewmoney="";
		for(var i=0;i<mymoneyArr.length;i++){
			if(i==0){
				mynewmoney=mymoneyArr[i];
				}
			else if(i==1){
				mynewmoney=mymoneyArr[i]+"."+mynewmoney;
				}
			else{
				mynewmoney=mymoneyArr[i]+","+mynewmoney;
				}
		}
	}
	else{
		if(index==1){
			mynewmoney="0.0"+mymoney;
			}
		else{
			mynewmoney="0."+mymoney;
			}
	}
	return '￥'+mynewmoney;
}

/* 
 * Highcharts support. 
 * right by Jiance Qin 
 * 
 * */
function drawPieChart(targetId, title, dataArray){
	$('#'+targetId).highcharts({
		credits: {
            enabled: false
        },
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: title
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.2f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b><br />{point.percentage:.2f} %'
                },
                showInLegend: true
            }
        },
        series: [{
            type: 'pie',
            name: '份额',
            data: dataArray
        }]
    });
}